//
//  projectLibraryVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/5/17.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "projectLibraryVC.h"
#import "projectLibraryCell.h"
#import "projectDetailVC.h"
#import "screenVC.h"
#import "SearchProjectInfoVC.h"
#import "SearchProjectCell.h"
#import "SearchProjectShaiXuan.h"

#import "LSBecomeVIPVC.h"
#import "LSRechargeVC.h"
#import "ForgetPassWord.h"
#import "LSPayResultVC.h"

static NSInteger page;
static NSMutableArray *dataArr;
static NSString *strTitle;
static NSString *projectCell = @"projectLibraryCell";
@interface projectLibraryVC ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>{

    NSString * seguStr;
    NSString * or_Number;
    BOOL  isSetPayPass;
    NSString * projectId;
    double price;

}
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (strong, nonatomic) NSMutableDictionary *notiDict;

@property (strong, nonatomic) LSPromptAlertVC * LSPromptAlert;
@end

@implementation projectLibraryVC

#pragma mark - 初始化

-(LSPromptAlertVC *)LSPromptAlert{

    if (!_LSPromptAlert) {
        _LSPromptAlert = [[LSPromptAlertVC alloc]init];
    }
    return _LSPromptAlert;

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    // 接收通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(shaiXuanNoti:) name:@"shaiXuanNoti" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ShaiXuanSearch:) name:@"SearchProjectShaiXuanNoti" object:nil];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
    //找出导航栏的下边的黑线
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = YES;
    [self sendRequest];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //self.title = @"项目库";
    page = 1;
    self.searchBar.delegate = self;
    self.searchBar.barTintColor = RGB(235, 235, 235);
    self.searchBar.layer.borderWidth = 1.f;
    self.searchBar.layer.borderColor = RGB(235, 235, 235).CGColor;
    NSArray *segmentedArray = [NSArray arrayWithObjects:@"PPP项目",@"普通项目",nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    segmentedControl.frame = CGRectMake(0.0, 0.0, 120, 25.0);
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.tintColor = RGB(44, 123, 235);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [segmentedControl addTarget:self  action:@selector(indexDidChangeForSegmentedControl:)
               forControlEvents:UIControlEventValueChanged];
    if ([self.tigStr isEqualToString:@"ppp"]) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.font = [UIFont systemFontOfSize:19];
        titleLabel.text = @"PPP项目";
        titleLabel.textColor = RGB(45, 45, 45);
        titleLabel.textAlignment = NSTextAlignmentCenter;
        self.navigationItem.titleView = titleLabel;
    }else{
        [self.navigationItem setTitleView:segmentedControl];
    }
    seguStr = @"getPppProjectList_app_ppp.im";
    // 集成上拉加载更多
    [self setupUpRefresh];
    
    // 集成下拉加载更多
    [self setupDownRefresh];

    [self setTableView];
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    if (!self.userId) {
        
        UIButton * commentBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44.0f, 44.0f)];
//        commentBtn.titleLabel.font = [UIFont systemFontOfSize:15];
//        [commentBtn setTitle:@"筛选" forState:UIControlStateNormal];
//        [commentBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [commentBtn addTarget:self action:@selector(rightBarButtonItemClick) forControlEvents:UIControlEventTouchUpInside];
//        UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc]initWithCustomView:commentBtn];
//        self.navigationItem.rightBarButtonItem = rightBtn;
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemClick)];
        rightItem.tintColor = RGB(45, 45, 45);
        self.navigationItem.rightBarButtonItem = rightItem;
    }
    
    
    //监听按钮
    [self.LSPromptAlert.PromptAlertView.becomeVIPBtn addTarget:self action:@selector(clickBecomeVIPBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self.LSPromptAlert.PromptAlertView.alipayBtn addTarget:self action:@selector(clickAlipayBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self.LSPromptAlert.PromptAlertView.balancePayBtn addTarget:self action:@selector(clickCashBtn) forControlEvents:UIControlEventTouchUpInside];
    
    //支付回调通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentView) name:@"alipayResult" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentFailedView) name:@"alipayFailed" object:nil];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

}
-(void)setupUpRefresh{
    
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(startBStableviewDownRrfresh)];
    
    // 马上进入刷新状态
    [self.tableview.mj_header beginRefreshing];
}
-(void)setupDownRefresh{
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    // 设置刷新图片
    UIImage *images = [UIImage imageNamed:@"mj_footer"];
    NSArray *refreshingImages = @[images];
    [footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多内容了" forState:MJRefreshStateRefreshing];
    footer.stateLabel.font = [UIFont systemFontOfSize:12.0f];
    footer.stateLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0f];
    
    [footer setAutomaticallyRefresh:NO];

    self.tableview.mj_footer = footer;

}
-(void)loadMoreData{
    
    page++;
    [self sendRequest];
    
}
-(void)startBStableviewDownRrfresh{
    if ([[USER_DEFAULTS objectForKey:@"isOpenSound"] boolValue]) {//声音开关
        NSString *path = [[NSBundle mainBundle]pathForResource:@"refresh"
                                                        ofType:@"wav"];
        
        SystemSoundID soundID;
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
        
        AudioServicesPlaySystemSound(soundID); //仅播放音频
    }

    strTitle = @"";
    dataArr = [NSMutableArray array];
    page = 1;
    [self sendRequest];
}
-(void)leftBarButtonItemClick{
   
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)rightBarButtonItemClick{
    if ([seguStr isEqualToString:@"getPppProjectList_app_ppp.im"]) {
    
        screenVC * screenvc = [[screenVC alloc]init];
        [self.navigationController pushViewController:screenvc animated:YES];
    
    }else{
    
        SearchProjectShaiXuan *sVC = [[SearchProjectShaiXuan alloc] init];
        
        [self.navigationController pushViewController:sVC animated:YES];
    
    }

}

#pragma mark - 通知方法
-(void)shaiXuanNoti:(NSNotification *)notification{
    self.notiDict = [notification.userInfo copy];
    dataArr = nil;
    dataArr  = [NSMutableArray array];
    [self sendRequest];
}
- (void)ShaiXuanSearch:(NSNotification *)dic

{
    self.notiDict = [NSMutableDictionary dictionaryWithDictionary:dic.userInfo];
    dataArr = nil;
    dataArr  = [NSMutableArray array];
    [self sendRequest];
    
}
-(void)setTableView{
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
}

#pragma mark - tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
   [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:dataArr.count];
    

        
        return dataArr.count;


}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:dataArr.count];
        
        return 1;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (dataArr.count > 0) {
        
        return 100;
        
    }else{
        
        return screenH;
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if ([seguStr isEqualToString:@"getPppProjectList_app_ppp.im"]) {
        return 10;
    }else{
    return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([seguStr isEqualToString:@"getPppProjectList_app_ppp.im"]) {
    
        // 注册
        UINib * nib = [UINib nibWithNibName:@"projectLibraryCell" bundle:nil];
        [self.tableview registerNib:nib forCellReuseIdentifier:projectCell];
        
        projectLibraryCell * cell = [self.tableview dequeueReusableCellWithIdentifier:projectCell];
        if (!cell) {
            cell = [[projectLibraryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:projectCell];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        
        NSDictionary *dataDcit = dataArr[indexPath.section];
        cell.titleLabel.text = dataDcit[@"title"];
        cell.industryLabel.text = dataDcit[@"industry"];
        cell.addressLabel.text = dataDcit[@"address"];
        
//        float floatValue = [dataDcit[@"investment"] floatValue];
        
        if ([[CustomExcetion comperNumber:dataDcit[@"investment"]] isEqualToString:@"0元"]) {
            cell.priceLabel.text = @"面议";
        }else{
            cell.priceLabel.text = [CustomExcetion comperNumber:dataDcit[@"investment"]];
        }
        //[NSString stringWithFormat:@"%.2f%@",[str2 floatValue],price];
        
        return cell;
    
    }else{
    
        static NSString *cellLoveSingle = @"cellSingle";
        //注册
        UINib * nib = [UINib nibWithNibName:@"SearchProjectCell" bundle:nil];
        
        [tableView registerNib:nib forCellReuseIdentifier:cellLoveSingle];
        SearchProjectCell *cell = [tableView dequeueReusableCellWithIdentifier:cellLoveSingle];
        tableView.rowHeight = UITableViewAutomaticDimension;
        
        if (!cell) {
            cell = [[SearchProjectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellLoveSingle];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        if (dataArr.count > 0) {
            
            cell.TitleLabel.text = dataArr[indexPath.section][@"title"];
            
            cell.AddressLabel.text = dataArr[indexPath.section][@"address"];
            
            cell.IndustryLabel.text = dataArr[indexPath.section][@"industry"];
           
            cell.PriceLabel.text = [CustomExcetion comperNumber:dataArr[indexPath.section][@"investment"]];
            
            if ([dataArr[indexPath.section][@"type"] isEqualToString:@"政府"]) {
                
                cell.ProjectIconImageView.image = [UIImage imageNamed:@"政府"];
            }else if ([dataArr[indexPath.section][@"type"] isEqualToString:@"企业"]){
                
                cell.ProjectIconImageView.image = [UIImage imageNamed:@"企业"];
            }else{
                
                cell.ProjectIconImageView.image = [UIImage imageNamed:@"个人"];
            }
            
        }
        
        return cell;
    }

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic = dataArr[indexPath.section];
            
            if ([seguStr isEqualToString:@"getPppProjectList_app_ppp.im"]) {
                
                projectDetailVC * prVC = [[projectDetailVC alloc]init];
                
                prVC.typeID = dic[@"Id"];
                
                prVC.userId = self.userId;
                
                [self.navigationController pushViewController:prVC animated:YES];
                
            }else{

                [self showRoundProgressWithTitle:@"请稍后..."];
                
                NSMutableDictionary * dict = [NSMutableDictionary dictionary];
                dict[@"projectId"] = dic[@"projectId"];
                dict[@"userId"] = [USER_DEFAULTS objectForKey:@"userid"];
                
                [LSHttpTool sendHttpData:@"projectDetail_app_project.im" params:dict success:^(id responseObject) {
                    NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
                    
                    if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
                        
                        [self.LSPromptAlert dismissViewControllerAnimated:YES completion:nil];
                    
                    SearchProjectInfoVC * SeaVC = [[SearchProjectInfoVC alloc]initWithNibName:@"SearchProjectInfoVC" bundle:nil];
                    SeaVC.dataDict = dicInfo[@"data"];
                    SeaVC.projectData = [NSMutableArray arrayWithArray:dicInfo[@"data"][@"commentsList"]];
                        
                    [self.navigationController pushViewController:SeaVC animated:YES];
                    
                    }else if ([[dicInfo objectForKey:@"result"] integerValue] == 2){
                    
                        [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];
                    
                    }else if ([[dicInfo objectForKey:@"result"]integerValue] == 3){
                        
                        isSetPayPass = [dicInfo[@"data"][@"isSetPayPass"] boolValue];
                        price = [dicInfo[@"data"][@"powerMoney"]floatValue];

                        projectId = dic[@"projectId"];
                        self.LSPromptAlert.modalPresentationStyle=UIModalPresentationOverCurrentContext;
                        [self presentViewController:self.LSPromptAlert animated:YES completion:nil];
                    
                    }
                    
                    [self hideBubble];
                } failure:^(NSError *error) {

                    [self showSingleIconBubble:@"网络连接错误"];
                    
                }];
                
                
            }

}

#pragma mark - 弹框按钮监听
//跳转会员界面
-(void)clickBecomeVIPBtn{

    [self.LSPromptAlert dismissViewControllerAnimated:YES completion:nil];
    
    LSBecomeVIPVC * LSBecomeVIP = [[LSBecomeVIPVC alloc]init];
    [self.navigationController pushViewController:LSBecomeVIP animated:YES];

}

#pragma mark - 支付回调通知
-(void)clickAlipayBtn{
    
    or_Number = [LSAlipay alipay:@"乐商网" body:@"项目详情" price:price];
    
}

-(void)presentFailedView{
    
    // 跳转至支付失败页面
    LSPayResultVC * LSPayResult = [[LSPayResultVC alloc]init];
    [LSPayResult setCashTypeWithCashFailed];
    [self.navigationController pushViewController:LSPayResult animated:YES];
    
}

-(void)presentView{
    
    [self.LSPromptAlert dismissViewControllerAnimated:YES completion:nil];
    [self showRoundProgressWithTitle:@"请稍后..."];
    
    NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
    dic[@"token"] = allConstMemberToken;
    dic[@"payUserId"] = allConstMemberid;
    dic[@"payWay"] = @"2";
    dic[@"payMoney"] = @(price);
    dic[@"resId"] = projectId;
    dic[@"order_no"] = or_Number;
    
    [LSHttpTool sendHttpData:@"buyProjectDetail_app_pay.im.lensum" params:dic success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];

        [self.LSPromptAlert dismissViewControllerAnimated:YES completion:nil];
        
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            
            NSMutableDictionary * dict = [NSMutableDictionary dictionary];
            dict[@"projectId"] = dic[@"resId"];
            dict[@"userId"] = [USER_DEFAULTS objectForKey:@"userid"];
            
            [LSHttpTool sendHttpData:@"projectDetail_app_project.im" params:dict success:^(id responseObject) {
                if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
                    
                    SearchProjectInfoVC * SeaVC = [[SearchProjectInfoVC alloc]initWithNibName:@"SearchProjectInfoVC" bundle:nil];
                    SeaVC.dataDict = responseObject[@"data"];
                    SeaVC.projectData = [NSMutableArray arrayWithArray:responseObject[@"data"][@"commentsList"]];
                    
                    [self.navigationController pushViewController:SeaVC animated:YES];
                    
                    [self hideBubble];
                    
                }else{

                    [self showSingleIconBubble:[responseObject objectForKey:@"msg"]];
                    
                }
                
            } failure:^(NSError *error) {

                [self showSingleIconBubble:@"网络连接错误"];
                
            }];
            
        }else{

           [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];
        }
        
    } failure:^(NSError *error) {
        
        // 跳转至支付成功页面
        LSPayResultVC * LSPayResult = [[LSPayResultVC alloc]init];
        [LSPayResult setCashTypeWithExchangeFailed];
        
        [self hideBubble];
    }];
    
}

#pragma mark - 余额支付
-(void)clickCashBtn{
    
    NSString * balanceStr = [self.LSPromptAlert.PromptAlertView.balanceLabel.text stringByReplacingOccurrencesOfString:@"￥"withString:@""];
    
    double balance = [balanceStr floatValue];
    
    if (balance < 1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付失败"
                                                        message:@"余额不足！"
                                                       delegate:self
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"立刻充值",nil];
        alert.tag = 10;
        [alert show];
        
    }else{
    
        if (isSetPayPass) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付失败"
                                                            message:@"未设置支付密码"
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"立即设置",nil];
            alert.tag = 20;
            [alert show];
            
        }else{
        
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"余额支付" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
            alert.alertViewStyle = UIAlertViewStylePlainTextInput;
            
            UITextField *title = [alert textFieldAtIndex:0];
            [title setSecureTextEntry:YES];
            title.borderStyle = UITextBorderStyleNone;
            title.textColor = [UIColor redColor];
            title.textAlignment = NSTextAlignmentCenter;
            title.font = [UIFont systemFontOfSize:16.0f];
            title.placeholder = @"请输入支付密码";
            
            
            [alert showAlertWithCompletionHandler:^(NSInteger buttonIndex) {
                if(buttonIndex == 1){
                    UITextField *tf=[alert textFieldAtIndex:0];
                    if (tf.text.length > 0) {
                        
                        [self showRoundProgressWithTitle:@"请稍后..."];
                        
                        NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
                        dic[@"token"] = allConstMemberToken;
                        dic[@"payUserId"] = allConstMemberid;
                        dic[@"payWay"] = @"1";
                        dic[@"payMoney"] = @(price);
                        dic[@"resId"] = projectId;
                        dic[@"payPass"] = tf.text;

                        
                        [LSHttpTool sendHttpData:@"buyProjectDetail_app_pay.im.lensum" params:dic success:^(id responseObject) {
                            
                            [self.LSPromptAlert dismissViewControllerAnimated:YES completion:nil];
                            
                            NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
                            
                            if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
                                
                                NSMutableDictionary * dict = [NSMutableDictionary dictionary];
                                dict[@"projectId"] = dic[@"resId"];
                                dict[@"userId"] = [USER_DEFAULTS objectForKey:@"userid"];
                                
                                [LSHttpTool sendHttpData:@"projectDetail_app_project.im" params:dict success:^(id responseObject) {
                                    if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
                                        
                                        SearchProjectInfoVC * SeaVC = [[SearchProjectInfoVC alloc]initWithNibName:@"SearchProjectInfoVC" bundle:nil];
                                        SeaVC.dataDict = responseObject[@"data"];
                                        SeaVC.projectData = [NSMutableArray arrayWithArray:responseObject[@"data"][@"commentsList"]];
                                        
                                        [self.navigationController pushViewController:SeaVC animated:YES];
                                        
                                        [self hideBubble];
                                        
                                    }else{

                                        [self showSingleIconBubble:[responseObject objectForKey:@"msg"]];
                                        
                                    }
                                    
                                } failure:^(NSError *error) {

                                    [self showSingleIconBubble:@"网络连接错误"];
                                    
                                }];
                                
                            }else{

                                [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];
                                
                            }
                            
                        } failure:^(NSError *error) {
                            
                            // 跳转至支付成功页面
                            LSPayResultVC * LSPayResult = [[LSPayResultVC alloc]init];
                            [LSPayResult setCashTypeWithCashFailed];
                            
                            [self hideBubble];
                        }];
                        
                    }else{
                        
                        NSString *strMsg = @"密码不能为空";
                        
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:strMsg delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
                        [alert show];
                        
                    }
                }
            }];

        
        }
    
    }

}

#pragma mark - alertview

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [self.LSPromptAlert dismissViewControllerAnimated:YES completion:nil];
        //充值
        LSRechargeVC * LSRecharge = [[LSRechargeVC alloc]init];
        
        switch (alertView.tag) {
            case 10:

                [self.navigationController pushViewController:LSRecharge animated:YES];
                
                break;
                
            case 20:
            {
            
                UIStoryboard *LoginAndRegister = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                ForgetPassWord *ForgetPassWord = [LoginAndRegister instantiateViewControllerWithIdentifier:@"forgetpassword"];
                ForgetPassWord.titleStr = @"修改支付密码";
                [self.navigationController pushViewController:ForgetPassWord animated:YES];
            
            }
                break;
                
            default:
                break;
        }
    }
    
    
}

#pragma mark - searchbar delegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    if (dataArr.count != 0) {
        dataArr = [NSMutableArray array];
        [self.tableview reloadData];
    }
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    //NSLog(@"%@",searchBar.text);
    
    if (searchBar.text.length == 0) {
        return YES;
    }else {
        page = 1;
        [self sendRequest];
        return YES;
    }
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    //NSLog(@"-----%@",searchBar.text);
    if (searchBar.text.length != 0) {
        [searchBar resignFirstResponder];
        page = 1;
        strTitle = searchBar.text;
        [self sendRequest];
        searchBar.text = @"";
    }
    
}
#pragma mark - segument
-(void)indexDidChangeForSegmentedControl:(id)sender{
    UISegmentedControl* control = (UISegmentedControl*)sender;
    switch (control.selectedSegmentIndex) {
        case 0:
            page =1;
            dataArr = [NSMutableArray array];
            seguStr = @"getPppProjectList_app_ppp.im";
            [self sendRequest];
//            NSLog(@"-----------我是PPP项目");
            break;
        case 1:
            page =1;
            dataArr = nil;
            dataArr = [NSMutableArray array];
            if (self.userId) {
                seguStr = @"getMyoutProject_app_myHome.im";
            }else{
                seguStr = @"getProject_app_project.im";
            }
            [self sendRequest];
//            NSLog(@"-----------我是普通项目");
            break;
            
        default:
            break;
    }
}
#pragma mark - 请求数据
-(void)sendRequest{
    NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
    
    if (self.notiDict) {
        dic = self.notiDict;
//        dic[@"page"] = @(1);
        
    }else{
        
        dic[@"page"] = @(page);
        dic[@"title"] = strTitle;
        dic[@"userId"] = self.userId;
    
    }

    [LSHttpTool sendHttpData:seguStr params:dic success:^(id responseObject) {
 
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        NSDictionary *dataDic = [NSDictionary dictionary];
//        
//        NSString * jsonStr = [LSHttpTool DataTOjsonString:dicInfo];
//        NSLog(@"--------------------json:%@",jsonStr);

        dataDic = dicInfo[@"data"];
        
        
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            if (page == 1) {
                dataArr = nil;
                dataArr = dataDic[@"record"];

                
            }else{
                NSArray *arr = dataDic[@"record"];
                
                NSMutableArray * mtArr = [NSMutableArray array];
                for (NSDictionary * dict in dataArr) {
                    [mtArr addObject:dict];
                }
                for (NSDictionary * dict in arr) {
                    [mtArr addObject:dict];
                }
                dataArr = nil;
                dataArr = mtArr;
                
            }
            [self.tableview reloadData];
            [self.tableview.mj_footer endRefreshing];
            [self.tableview.mj_header endRefreshing];
            
        }else{
            self.notiDict = nil;
            [self.tableview reloadData];
            self.tableview.rowHeight = screenH;
            [self.tableview.mj_footer endRefreshing];
            [self.tableview.mj_header endRefreshing];
        }

    } failure:^(NSError *error) {
        self.notiDict = nil;
        [self showSingleIconBubble:@"网络连接错误"];

        [self.tableview.mj_footer endRefreshing];
        [self.tableview.mj_header endRefreshing];
    }];
}

@end
